/*
 * This file is part of Annuaire.
 *
 * Annuaire is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Annuaire is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Annuaire.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.miage.annuaire;

import java.util.LinkedList;
import java.util.List;

public class AnnuaireUtil {

	private AnnuaireUtil() {
		// Rien.
	}

	public List<Membre> getCommonMembers(List<Membre>... lists) {

		int length = lists.length;
		
		List<Membre> membres = new LinkedList<Membre>();
		List<Membre> result = new LinkedList<Membre>();

		for (List<Membre> list : lists) {
			if (list != null) {
				membres.addAll(list);
			} else {
				length--;
			}
		}

		for (Membre m : membres) {
			if (this.countMembre(m, membres) == length
					&& !result.contains(m)) {
				result.add(m);
			}
		}

		return result;
	}

	private int countMembre(Membre membre, List<Membre> membres) {
		int count = 0;
		for (Membre m : membres) {
			if (membre.equals(m)) {
				count++;
			}
		}
		return count;
	}

	public static AnnuaireUtil getInstance() {
		return instance;
	}

	private static AnnuaireUtil instance = new AnnuaireUtil();

}
